package org.boot.ssm.wxshop.web.controller;

import com.github.pagehelper.PageInfo;
import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.domain.UserLevel;
import org.boot.ssm.wxshop.service.UserLevelService;
import org.boot.ssm.wxshop.service.UserService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: wxshop
 * @description: 用户等级控制器
 * @author: 刘涛
 * @create: 2018-03-15 11:20
 **/
@Controller
@RequestMapping(value = "/userlevel")
public class UserLevelController {

    @Autowired
    private UserLevelService userLevelService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/goUserLevelPage")
    /*****************************
    * @Description: 去到显示用户级别列表页面
    * @Param: [] 
    * @return: java.lang.String 
    * @Author: 刘涛 
    * @Date: 2018/3/15 
    *******************************/
    public String goUserLevelPage(){
        return "backstage/user/userLevel/userlevellist";
    }

    @RequestMapping(value = "/goAddOrUpdateUserLevelPage")
    /*****************************
     * @Description: 去到添加用户级别列表页面
     * @Param: []
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/15
     *******************************/
    public String goAddOrUpdateUserLevelPage(){
        return "backstage/user/userLevel/addorupdatelevel";
    }


    @RequestMapping(value = "/goAddOrUpdateUserLevelPageForId/{id}")
    /*****************************
     * @Description: 去到修改用户级别列表页面
     * @Param: []
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/15
     *******************************/
    public String goAddOrUpdateUserLevelPageForId(@PathVariable(name = "id")Integer id, Model model){
        model.addAttribute("userLevel",userLevelService.queryById(id));
        return "backstage/user/userLevel/addorupdatelevel";
    }


    @RequestMapping(value = "/listLevel")
    @ResponseBody
    /*****************************
    * @Description: 获取用户等级分页信息
    * @Param: [pageNum, pageSize, userLevel] 
    * @return: org.boot.ssm.wxshop.web.common.JsonResult 
    * @Author: 刘涛 
    * @Date: 2018/3/15 
    *******************************/
    public JsonResult listLevel(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize, UserLevel userLevel) {
        PageInfo<UserLevel> pageInfo = userLevelService.selectAllForPage(pageNum, pageSize, userLevel);
        return new JsonResult(0, "加载成功", pageInfo.getList(), pageInfo.getTotal());
    }

    @RequestMapping(value = "/verifyLevelName")
    @ResponseBody
    /*****************************
    * @Description: 用户等级名称验证
    * @Param: [userLevel] 
    * @return: org.boot.ssm.wxshop.web.common.JsonResult 
    * @Author: 刘涛 
    * @Date: 2018/3/15 
    *******************************/
    public JsonResult verifyLoginName(UserLevel userLevel) {
        return userLevelService.verifyLevelName(userLevel);
    }

    @RequestMapping(value = "/saveOrUpdateLevelInBack")
    @ResponseBody
    /*****************************
     * @Description: 后台管理界面添加或修改用户
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult<java.lang.String>
     * @Author: 刘涛
     * @Date: 2018/3/15
     *******************************/
    public JsonResult saveOrUpdateLevelInBack(UserLevel userLevel) {
            return userLevelService.addOrUpdateLevel(userLevel);
    }

    @RequestMapping(value = "/isDeleteLevel")
    @ResponseBody
    /*****************************
    * @Description: 逻辑删除用户等级 
    * @Param: [userLevel, user] 
    * @return: org.boot.ssm.wxshop.web.common.JsonResult 
    * @Author: 刘涛 
    * @Date: 2018/3/15 
    *******************************/
    public JsonResult isDeleteLevel(UserLevel userLevel,User user){
        user.setId(null);
        return userLevelService.isDeleteLevel(userLevel,userService.queryCount(user));
    }


}
